/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clases;

import java.sql.*;
/**
 *
 * @author Ari Mendez
 */
public class ControlDB {
    public Connection conexion;
    static String bd="default_schema";
    static String user="root";
    static String password="jhonaA01";
    static String server="jdbc:mysql://localhost:3306/proyecto";
    
    public ControlDB(){
        conexion=null;
    }
    
    public void conectar(){
        try{
            Class.forName("com.mysql.jdbc.Driver");
            conexion = DriverManager.getConnection(server,user,password);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
    
    public ResultSet obtenerConsulta(String consulta){
        if(conexion==null)
            conectar();
        ResultSet result=null;
        try{
            Statement state=conexion.createStatement();
            result=state.executeQuery(consulta);
            //state.close();
        }catch(Exception ex){
            result=null;
        }
        return result;
    }
    
    public boolean ejecutarComando(String comando){
        if(conexion==null)
            conectar();
        try{
            Statement state=conexion.createStatement();
            if(!state.execute(comando)){
                System.out.println("Fallo al ejecutar el comando");
                state.close();
                return false;
            }
            state.close();
            return true;
        }catch(Exception ex){
            ex.printStackTrace();
            return false;
        }
    }
    
    public void cerrarConexion(){
        if(conexion!=null){
            try{
                conexion.close();
            }catch(Exception ex){}
            conexion=null;
        }
    }
}
